﻿using Furion.DependencyInjection;
using LS.Nc6.Application.System;
using LS.Nc6.Core;
using LS.Nc6.Web.Entry.ViewModel.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections;


namespace LS.Nc6.Web.Entry.Controllers
{
    public class CustomController : Controller
    {
        private readonly ICmsChannelService _cmsChannelService;
        private readonly ICmsContentService _contentService;
        private readonly ISysFileService _fileService;
        private  readonly ICache _cache;

        
        public CustomController(ICmsChannelService cmsChannelService,
            ISysFileService fileService,
            ICmsContentService contentService, 
            IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed) 
        {
            _cmsChannelService = cmsChannelService;
            _contentService = contentService;
            _fileService= fileService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }

        /// <summary>
        /// 联系我们
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Contactus(long id)
        {
            var channelmodel = await _cmsChannelService.GetByIdAsync(id);
            if (channelmodel == null) return RedirectToAction("NotFound", "Home");

            var contentmodel = await _contentService.GetModelAsync(a => a.ChannelId == channelmodel.Id);
            ViewBag.Title = channelmodel.ChannelName;

            CmsContentVm cmsContentVm = new CmsContentVm();
            cmsContentVm.ChildChanName = channelmodel.ChannelName;
            cmsContentVm.ChanIndex = channelmodel.IndexName;
            if (channelmodel.ParentId != 1)
            {
                var pchannelmodel = await _cmsChannelService.GetByIdAsync(channelmodel.ParentId.Value);
                cmsContentVm.ChanName = pchannelmodel.ChannelName;
            }
            else
            {
                cmsContentVm.ChanName = channelmodel.ChannelName;
            }
            cmsContentVm.ChanBackground = !string.IsNullOrEmpty(channelmodel.ImageUrlIds) ? (await _fileService.GetByIdAsync(CommonUtil.GetLongList(channelmodel.ImageUrlIds)[0]))?.Fpath : "ui/image/channlbg.jpg";
         
            if (contentmodel != null)
            {
                cmsContentVm.Id = contentmodel.Id;
                cmsContentVm.Title = contentmodel.Title;
                cmsContentVm.SubTitle = contentmodel.SubTitle;
                cmsContentVm.Summary = contentmodel.Summary;
                cmsContentVm.AddTime = contentmodel.AddTime.ToString("yyyy-MM-dd");
                cmsContentVm.Body = contentmodel.Body;
            }
            
            return View(cmsContentVm);
        }
      


        #region 公有方法



        
        


        #endregion

        #region 私有方法
     
        #endregion

    }
}